package com.liehu.sms.util;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;

import com.tencentcloudapi.sms.v20190711.SmsClient;
import com.tencentcloudapi.sms.v20190711.models.*;
import org.springframework.stereotype.Component;;

/**
 * 工具类，封装了腾讯云的短信发送服务的相关信息
 */
@Component
public class SendSmsUtil
{
    //产品名称:腾讯云应用的通用Id
    static final String secretId = "AKIDpQMCnEHzQbV8fyKmUzLt9G8j7VeXgl8H";
    //产品名称:腾讯云应用的通用Id的密匙
    static final String SecretKey = "1lLo15sP6bgUBcmgVVIp48MLMc4RXRAe";
    //产品域名,开发者无需替换
    static final String domain = "sms.tencentcloudapi.com";
    //模板的id，通过腾讯云后台查看
    static final String TemplateID = "844073";
    //短信SdkAppid在 [短信控制台] 添加应用后生成的实际SdkAppid
    static final String SmsSdkAppid  = "1400475029";
    //短信签名内容，使用 UTF-8 编码，必须填写已审核通过的签名，签名信息可登录
    static final String Sign  = "猎狐人才";

    public SendSmsRequest sendSms(String mobile,String checkCode){
       SendSmsRequest req = new SendSmsRequest();//提取对象
       try{
           Credential cred = new Credential(secretId, SecretKey); //创建条件
           HttpProfile httpProfile = new HttpProfile();//发起请求条件
           httpProfile.setEndpoint(domain);//产品域名
           ClientProfile clientProfile = new ClientProfile();
           clientProfile.setHttpProfile(httpProfile);
           SmsClient client = new SmsClient(cred, "", clientProfile);
           String[] phoneNumberSet1 = {"+86"+mobile};//需要发送验证码的手机号码
           req.setPhoneNumberSet(phoneNumberSet1);
           String[] templateParamSet1 = {checkCode};//发送的验证码
           req.setTemplateParamSet(templateParamSet1);
           req.setTemplateID(TemplateID);//基础信息TemplateID
           req.setSmsSdkAppid(SmsSdkAppid);//基础信息SmsSdkAppid
           req.setSign(Sign);//基础信息Sign

           SendSmsResponse resp = client.SendSms(req);
           String result = SendSmsResponse.toJsonString(resp);
           //  System.out.println(SendSmsResponse.toJsonString(resp));
       } catch (TencentCloudSDKException e) {
           System.out.println(e.toString());
       }
       return req;
    }

}